package cc.android.testapp.ui;

import android.app.Activity;
import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import cc.android.testapp.R;
import cc.commons.util.IOUtil;
import cc.commons.util.reflect.MethodUtil;

public class CameraActivity extends AppCompatActivity implements View.OnClickListener {

    public static final int REQUEST_IMAGE = 0;
    public static final int CAPTURE_IMAGE = 1;

    protected TextView mImagePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);

        this.mImagePath = (TextView) findViewById(R.id.textView_show_image_path);

        this.findViewById(R.id.btn_select_image).setOnClickListener(this);
        this.findViewById(R.id.btn_capture_image).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v instanceof Button) {
            Button btn = (Button) v;
            if (btn.getId() == R.id.btn_select_image) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*");
                startActivityForResult(intent, REQUEST_IMAGE);
            } else if (btn.getId() == R.id.btn_capture_image) {
                Intent intent = (Intent) getIntent().clone();
                intent.setClassName("com.android.camera", "com.android.camera.Camera");
                if (Build.VERSION.SDK_INT >= 26) {
                    intent.removeFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
                }
                startActivityForResult(intent, CAPTURE_IMAGE);
            }
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case REQUEST_IMAGE:
                    if (data != null && data.getData() != null) {
                        Intent tIntent = new Intent();
                        tIntent.setData(data.getData());
                        this.setResult(Activity.RESULT_OK, tIntent);

                        if (getIntent().getExtras() != null) {
                            Object tOut = getIntent().getExtras().get(MediaStore.EXTRA_OUTPUT);
                            if (tOut instanceof Uri) {
                                copyUri(data.getData(), (Uri) tOut);
                            }
                        }

                        ClipData tClipData = getIntent().getClipData();
                        if ((tClipData != null && tClipData.getItemCount() > 0)) {
                            ClipData.Item tItem = tClipData.getItemAt(0);
                            if (tItem.getUri() != null) {
                                copyUri(data.getData(), tItem.getUri());
                            }
                        }
                        finish();
                    } else {
                        Toast.makeText(this, "图片损坏，请重新选择", Toast.LENGTH_SHORT).show();
                    }

                    break;
                case CAPTURE_IMAGE:
                    this.setResult(Activity.RESULT_OK, data);
                    finish();
                    break;
            }
        }
    }

    private void copyUri(Uri pFrom, Uri pTo) {
        InputStream tIStream = null;
        OutputStream tOStream = null;
        try {
            tIStream = getContentResolver().openInputStream(pFrom);
            tOStream = getContentResolver().openOutputStream(pTo);
            IOUtil.copy(tIStream, tOStream);
        } catch (IOException e) {
            Toast.makeText(this, "无法存储文件到指定缓存", Toast.LENGTH_SHORT).show();
        } finally {
            IOUtil.closeStream(tIStream, tOStream);
        }
    }
}